package com.pf.utils;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.PrintWriter;

/**
 * 比较重要的类
 * 利用 ByteArrayOutputStream 将输入流转为输出流
 *
 * ByteArrayOutputStream
 * ByteArrayInputStream
 * ByteArrayResource
 *
 * @author PF
 * @date 2021/7/1 22:25
 * @Version 1.0
 */
public class MyFileUtils {
    public static void main(String[] args) throws Exception {
        // 1.字符流读取文件信息
        //readFileContent();

        // 2.将文件对象转为字节
        byte[] bytes = file2Byte();
        System.out.println(new String(bytes));

        // 3.字节转为输入流
        bytes2InputStream();

        // 4.字节转为文件对象
        bytes2File();
    }

    /**
     * 利用字符流读取文件信息
     *
     * @throws Exception
     */
    public static void readFileContent() throws Exception {
        // 创建文件对象
        File file = new File("E:\\a.txt");
        // fileReader 对象读取文件
        FileReader fileReader = new FileReader(file);
        // 将文件信息读取到缓冲区
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String content = "";
        while ((content = bufferedReader.readLine()) != null) {
            // 打印文件内容
            System.out.println(content);
        }
    }

    /**
     * 将文件对象转为字节
     */
    public static byte[] file2Byte() throws Exception {
        // 创建文件对象
        File file = new File("E:\\a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int length = -1;
        while ((length = fileInputStream.read(b)) != -1) {
            byteArrayOutputStream.write(b, 0, length);
        }

        // 将其转为字节
        byte[] bytes = byteArrayOutputStream.toByteArray();
        return bytes;
    }

    /**
     * 字节转为输入流
     *
     * @throws Exception
     */
    public static void bytes2InputStream() throws Exception {
        byte[] bytes = file2Byte();
        // 字节转为InputStream
        InputStream inputStream = new ByteArrayInputStream(bytes);
    }

    /**
     * 字节转为文件对象
     *
     * @throws Exception
     */
    public static void bytes2File() throws Exception {
        byte[] bytes = file2Byte();

        // 创建文件
        File file = new File("E:\\b.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bufferedOutputStream.write(bytes);
        bufferedOutputStream.flush();
        if(fileOutputStream != null){
            fileOutputStream.close();
        }
        if(bufferedOutputStream != null){
            bufferedOutputStream.close();
        }
    }


    /**
     * 输出流转为输入流
     * 通过ByteArrayOutputStream对象把输出转换为字节数组
     * 通过ByteArrayInputStream对象把字节数组转换为输入流
     */
    public static void outStream2InputStream() throws Exception{
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        PrintWriter ps = new PrintWriter(output);

        for (int i = 0; i < 1000; i++) {
            ps.printf("This is line %d\n", i);
        }
        ps.close();

        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = input.read(buffer)) > 0) {
            System.out.write(buffer, 0, len);
        }
        input.close();
    }

}
